import { ColDef, ICellRendererParams } from 'ag-grid-community'
import { AdCellTemplateComponent } from '../components/cell-template/cell-template.component'
import { CheckInfo, Delete, Download, Edit, RenderTemplateType } from './grid.interface'
import { DomSanitizer } from '@angular/platform-browser'
import { SecurityContext } from '@angular/core'
import { Utils } from 'monk-design/utils'
import { InjectorModule } from 'monk-design/core'
export type CellRenderTitleCallback = (params: ICellRendererParams, multiFieldValue?: any) => any

export class AdGridUtils {
  /**
   * 合并配置列参数
   * @param columnConfig
   * @param defaultConfig
   * @returns
   */
  static mergeOptions(columnConfig: ColDef[], defaultConfig: ColDef): ColDef[] {
    return columnConfig.map((item) => {
      let cloneDefaultConfig = { ...defaultConfig }
      if (['last-column-placeholder', 'checkbox'].includes(item.field)) {
        return item
      }
      if (item.cellRendererFramework) {
        // eslint-disable-next-line
        const { cellRenderer, ...excludeCellRendererDefaultConfig } = cloneDefaultConfig
        cloneDefaultConfig = excludeCellRendererDefaultConfig
      }
      return Object.assign({ suppressMovable: true }, cloneDefaultConfig, item)
    })
  }

  /**
   * 生成多选框配置列
   * @returns
   */
  static checkbox(): ColDef {
    return {
      headerCheckboxSelection: true,
      checkboxSelection: true,
      suppressSizeToFit: true,
      sortable: false,
      resizable: false,
      colId: 'checkbox',
      field: 'checkbox',
      width: 50,
      minWidth: 50,
      maxWidth: 50,
    }
  }

  /**
   * 序号
   * @returns
   */
  static serialNumber() {
    return {
      headerName: '序号',
      field: 'serial-number',
      colId: 'serial-number',
      suppressSizeToFit: true,
      suppressMovable: true,
      width: 100,
      minWidth: 100,
      maxWidth: 100,
      resizable: false,
      sortable: false,
      ...AdGridUtils.cellRenderTitle((params) => params.rowIndex + 1),
    }
  }

  /**
   * 模板渲染
   * @param cellRenderList
   * @returns
   */
  static cellTemplate<T = any, D = any>(
    ...cellRenderList: Array<RenderTemplateType<T, D>>
  ): ColDef {
    return {
      cellRenderer: null,
      cellRendererFramework: AdCellTemplateComponent,
      cellRendererParams: {
        cellRenderList,
      },
    }
  }

  /**
   * 文字单元格
   * @param cellRender
   * @returns
   */
  static title<T = any, D = any>(
    cellRender: RenderTemplateType<T, D> = {}
  ): RenderTemplateType<T, D> {
    return {
      value: (cell) => cell.value,
      ...cellRender,
    } as RenderTemplateType<T, D>
  }

  /**
   * 主要色文字单元格
   * @param cellRender
   * @returns
   */
  static primary<T extends any = any, D = any>(
    cellRender: RenderTemplateType<T, D> = {}
  ): RenderTemplateType<T, D> {
    return AdGridUtils.title<T, D>({
      click: (cell) => console.log(`cell click invoke`, cell),
      class: 'primary',
      ...cellRender,
    })
  }

  /**
   * 主要色文字单元格
   * @param cellRender
   * @returns
   */
  static danger<T extends any = any, D = any>(
    cellRender: RenderTemplateType<T, D> = {}
  ): RenderTemplateType<T, D> {
    return AdGridUtils.title<T, D>({
      click: (cell) => console.log(`cell click invoke`, cell),
      class: 'danger',
      ...cellRender,
    })
  }

  /**
   * 查看详情
   * @param cellRender
   * @returns
   */
  static checkInfo<T extends CheckInfo<T, D>, D = any>(
    cellRender: RenderTemplateType<T, D> = {}
  ): RenderTemplateType<T, D> {
    return {
      class: ['iconfont', 'FCxiangqing', 'px-24'],
      click: (cell) => {
        cell.componentParent.checkInfo?.(cell)
      },
      value: '',
      title: '详情',
      ...cellRender,
    } as RenderTemplateType<T, D>
  }

  /**
   * 删除
   * @param cellRender
   * @returns
   */
  static delete<T extends Delete<T, D>, D = any>(
    cellRender: RenderTemplateType<T, D> = {}
  ): RenderTemplateType<T, D> {
    return {
      class: ['iconfont', 'FCshanchu', 'px-22'],
      click: (cell) => cell.componentParent.delete?.(cell),
      value: '',
      title: '删除',
      ...cellRender,
    } as RenderTemplateType<T, D>
  }

  /**
   * 下载
   * @param cellRender
   * @returns
   */
  static download<T extends Download<T, D>, D = any>(
    cellRender: RenderTemplateType<T, D> = {}
  ): RenderTemplateType<T, D> {
    return {
      title: '下载',
      value: '',
      click: (cell) => cell.componentParent.download?.(cell),
      class: ['iconfont', 'FCxiazai1', 'px-22'],
      ...cellRender,
    } as RenderTemplateType<T, D>
  }

  /**
   * 编辑
   * @param cellRender
   * @returns
   */
  static edit<T extends Edit<T, D>, D = any>(
    cellRender: RenderTemplateType<T, D> = {}
  ): RenderTemplateType<T, D> {
    return {
      title: '编辑',
      value: '',
      click: (cell) => cell.componentParent.edit?.(cell),
      class: ['iconfont', 'FCbianji-2', 'px-22'],
      ...cellRender,
    } as RenderTemplateType<T, D>
  }

  static cellRenderTitleFunc(params: ICellRendererParams, multiFieldValue?: any): any {
    return multiFieldValue == null ? params.value : multiFieldValue
  }

  /**
   * title渲染
   * @param render
   * @returns
   */
  static cellRenderTitle(
    render: CellRenderTitleCallback = AdGridUtils.cellRenderTitleFunc,
    ...fields: string[]
  ): ColDef {
    return {
      cellRendererFramework: null,
      cellRenderer: (params) => {
        const multiFieldValue = Utils.getValueByKeys(params.data, fields)
        const value = render(params, multiFieldValue)
        if (value === null || value === undefined) {
          return ''
        }
        return InjectorModule.injector
          .get(DomSanitizer)
          .sanitize(SecurityContext.HTML, `<span title="${value}">${value}</span>`)
      },
    }
  }
}
